<template>
  <div class="container" v-if="!loading">
    <a-row style="margin: 16px 0" :gutter="24">
      <a-col :span="12">
        <LineChart :data="userData" title="近30日新增用户统计" />
      </a-col>
      <a-col :span="12">
        <LineChart :data="announcementData" title="近30日新增公告统计" />
      </a-col>
    </a-row>
    <a-row style="margin: 16px 0" :gutter="24">
      <a-col :span="12">
        <LineChart :data="fileInfoData" title="近30日新增文件统计" />
      </a-col>
      <a-col :span="12">
        <LineChart :data="userSignData" title="近30日新增签到统计" />
      </a-col>
    </a-row>
  </div>
</template>

<script lang="ts" setup>
import useLoading from '@/hooks/useLoading.ts';
import { dayNewlyAddedUserUsingPost } from '@/service/backend/userController';
import { dayNewlyAddedAnnouncementUsingPost } from '@/service/backend/announcementController';
import { dayNewlyAddedFileUsingPost } from '@/service/backend/fileInfoController';
import { dayNewlyAddedUserSignUsingPost } from '@/service/backend/userSignController';
import { onMounted, ref } from 'vue';
import { Message } from '@arco-design/web-vue';
import LineChart from './components/LineChart.vue';

const { loading, setLoading } = useLoading(true);
const userData = ref([]);
const announcementData = ref([]);
const fileInfoData = ref([]);
const userSignData = ref([]);

/**
 * 加载用户统计数据
 */
const loadUserStatistics = async () => {
  try {
    const { data, code } = await dayNewlyAddedUserUsingPost();
    if (code === 0 && data) {
      userData.value = data;
    }
  } catch (e: any) {
    Message.error(e.message);
  }
};

/**
 * 加载公告统计数据
 */
const loadAnnouncementStatistics = async () => {
  try {
    const { data, code } = await dayNewlyAddedAnnouncementUsingPost();
    if (code === 0 && data) {
      announcementData.value = data;
    }
  } catch (e: any) {
    Message.error(e.message);
  }
};

/**
 * 加载文件统计数据
 */
const loadFileInfoStatistics = async () => {
  try {
    const { data, code } = await dayNewlyAddedFileUsingPost();
    if (code === 0 && data) {
      fileInfoData.value = data;
    }
  } catch (e: any) {
    Message.error(e.message);
  }
};

/**
 * 加载用户签到统计数据
 */
const loadUserSignStatistics = async () => {
  try {
    const { data, code } = await dayNewlyAddedUserSignUsingPost();
    if (code === 0 && data) {
      userSignData.value = data;
    }
  } catch (e: any) {
    Message.error(e.message);
  }
};

onMounted(async () => {
  setLoading(true);
  try {
    await loadUserStatistics();
    await loadAnnouncementStatistics();
    await loadFileInfoStatistics();
    await loadUserSignStatistics();
  } finally {
    setLoading(false);
  }
});
</script>

<style scoped lang="less">
.container {
  padding: 0 20px 20px;
}
</style>
